import GlobalConstPkg from '../../common/globalConst.js'
var global = GlobalConstPkg.GlobalConst;
import utilfuncs from './utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import LGXCOM from '../common.js'
import coord from '../struct/coord.js';
import { LGXEnums } from '../sysenum.js';
var posEnums = LGXEnums.LGXPosMode;

var PositionUtil = {}

PositionUtil.rotatePos = function (pos, rotCntPt, rotAngle) {
    switch (pos.posType) {
        case posEnums.multiPt_Mode:
            this.rotateMultiPointPos(pos, rotCntPt, rotAngle);
            break;

        case posEnums.arc_mode:
            this.rotateArcPos(pos, rotCntPt, rotAngle);
            break;

        case posEnums.circle_mode:
            this.rotateCirclePos(pos, rotCntPt, rotAngle);
            break;

        case posEnums.rect_mode:
            this.rotateRectPos(pos, rotCntPt, rotAngle);
            break;

        case posEnums.point_mode:
            this.rotatePoint(pos, rotCntPt, rotAngle);
            break;

        case posEnums.ellipseArc_mode:
            this.rotateEllipseArcPos(pos, rotCntPt, rotAngle);
            break;

        case posEnums.ellipse_mode:
            this.rotateEllipsePos(pos, rotCntPt, rotAngle);
            break;

        default:
            break;

    }
    return 0;
}

PositionUtil.rotatePoint = function (pos, rotCntPt, rotAngle) {
    let pt = { x: pos.x, y: pos.y }
    let mrPt = funcs.rotate(pt, rotCntPt, rotAngle);
    pos.x = mrPt.x;
    pos.y = mrPt.y;
}

PositionUtil.rotateMultiPointPos = function (pos, rotCntPt, rotAngle) {
    let posList = pos.posList;
    for (let i = 0; i < posList.length; i++) {
        let pt = posList[i];
        let rotPt = funcs.rotate(pt, rotCntPt, rotAngle);
        pt.x = rotPt.x;
        pt.y = rotPt.y;
    }
}


PositionUtil.rotateArcPos = function (pos, rotCntPt, rotAngle) {
    let cnt = { x: pos.x, y: pos.y };
    let x = cnt.x + pos.radius * Math.cos(funcs.degree2Radian(pos.stAngle));
    let y = cnt.y + pos.radius * Math.sin(funcs.degree2Radian(pos.stAngle));
    let stPoint = { x, y };
    x = cnt.x + pos.radius * Math.cos(funcs.degree2Radian(pos.endAngle));
    y = cnt.y + pos.radius * Math.sin(funcs.degree2Radian(pos.endAngle));
    let endPoint = { x, y };

    let rotArcCnt = funcs.rotate(cnt, rotCntPt, rotAngle);
    let rotStPoint = funcs.rotate(stPoint, rotCntPt, rotAngle);
    let rotEndPoint = funcs.rotate(endPoint, rotCntPt, rotAngle);

    let vector1 = funcs.getVector(rotArcCnt, rotStPoint);
    let vector2 = funcs.getVector(rotArcCnt, rotEndPoint);

    pos.stAngle = vector1.angleFrmXAxisPositiveDir();
    pos.endAngle = vector2.angleFrmXAxisPositiveDir();
    pos.x = rotArcCnt.x;
    pos.y = rotArcCnt.y;
}


PositionUtil.rotateCirclePos = function (pos, rotCntPt, rotAngle) {
    let pt = { x: pos.x, y: pos.y }
    let mrPt = funcs.rotate(pt, rotCntPt, rotAngle);
    pos.x = mrPt.x;
    pos.y = mrPt.y;
}

PositionUtil.rotateRectPos = function (pos, rotCntPt, rotAngle) {

}

PositionUtil.rotateEllipseArcPos = function (pos, rotCntPt, rotAngle) {
    let cnt = { x: pos.x, y: pos.y };
    let rotArcCnt = funcs.rotate(cnt, rotCntPt, rotAngle);
    pos.x = rotArcCnt.x;
    pos.y = rotArcCnt.y;
}

PositionUtil.rotateEllipsePos = function (pos, rotCntPt, rotAngle) {
    let cnt = { x: pos.x, y: pos.y };
    let rotArcCnt = funcs.rotate(cnt, rotCntPt, rotAngle);
    pos.x = rotArcCnt.x;
    pos.y = rotArcCnt.y;
}

PositionUtil.cloneSrcPos = function (srcPos) {
    let newPos = null;
    switch (srcPos.posType) {
        case LGXEnums.LGXPosMode.multiPt_Mode:
            newPos = new coord.MultiPos();
            break;

        case LGXEnums.LGXPosMode.arc_mode:
            newPos = new coord.ArcPos();
            break;

        case LGXEnums.LGXPosMode.circle_mode:
            newPos = new coord.CirclePos();
            break;

        case LGXEnums.LGXPosMode.point_mode:
            newPos = new coord.PointPos();
            break;

        case LGXEnums.LGXPosMode.rect_mode:
            newPos = new coord.RectPos();
            break;

        case LGXEnums.LGXPosMode.ellipseArc_mode:
            newPos = new coord.EllipseArcPos();
            break;

        case LGXEnums.LGXPosMode.ellipse_mode:
            newPos = new coord.EllipsePos();
            break;

        default:
            break;
    }
    if (newPos) {
        newPos.clone(srcPos);
    }
    return newPos;
}

export { PositionUtil }